package com.lvtongka.app.dialog

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.view.*
import android.widget.Toast
import com.lvtongka.app.R
import com.lvtongka.app.databinding.DialogAuthorizeBinding
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.common.WebConstants
import com.lvtongka.app.ui.webview.WebViewActivity

open class AuthorizeDialog(context: Context, theme: Int) : Dialog(context, theme) {
    private var binding: DialogAuthorizeBinding = DialogAuthorizeBinding.inflate(layoutInflater)
    private var isPayManagerAgreement: Boolean = false

    init {
        window!!.decorView.setPadding(0, 0, 0, 0)
        window!!.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION or Gravity.BOTTOM)
        val lp = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        window!!.attributes = lp
        setContentView(binding.root)
        initView()
        initListener()
    }

    open fun hideDialog() {
        cancel()
        dismiss()
    }

    private fun initView() {

    }


    private fun initListener() {
        setOnKeyListener { _, keyCode, event ->
            false
        }
        binding.imgBack.setOnClickListener { hideDialog() }
        binding.ivAgreementAuto.setOnClickListener {
            isPayManagerAgreement = !isPayManagerAgreement
            SPHelper.putBoolean(SPKConstants.PAY_MANAGER_AGREEMENT, isPayManagerAgreement)
            if (isPayManagerAgreement) {
                binding.ivAgreementAuto.setBackgroundResource(R.mipmap.ic_chekboxes_selected)
            } else {
                binding.ivAgreementAuto.setBackgroundResource(R.mipmap.ic_chekboxes_unselected)
            }
        }

        binding.tvAgreementAuto.setOnClickListener {
            isPayManagerAgreement = !isPayManagerAgreement
            SPHelper.putBoolean(SPKConstants.PAY_MANAGER_AGREEMENT, isPayManagerAgreement)
            if (isPayManagerAgreement) {
                binding.ivAgreementAuto.setBackgroundResource(R.mipmap.ic_chekboxes_selected)
            } else {
                binding.ivAgreementAuto.setBackgroundResource(R.mipmap.ic_chekboxes_unselected)
            }
        }
        binding.tvAgreement.setOnClickListener {
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra(
                WebViewActivity.URL_KEY, WebConstants.PRIVACY_AGREEMENT
            )
            intent.putExtra(WebViewActivity.IS_WITHE, true)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
        binding.tvNext.setOnClickListener {
            if (!isPayManagerAgreement) {
                Toast.makeText(context, "请阅读并同意用户授权协议", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            mListener?.onConfirm()
        }
    }

    private var mListener: OnConfirmClickListener? = null

    interface OnConfirmClickListener {
        fun onConfirm()
    }

    fun setOnItemClickListener(listener: OnConfirmClickListener) {
        this.mListener = listener
    }

}